<template>
	<view class="__pd_page_wrapper" :class="{
		'__padding_custom_header': widthCustomHeaderPadding,
	}">
		<slot></slot>
	</view>
	<!-- 隐私协议 -->
	<pd-privacy ref='pricavy' @success="privacyRead"></pd-privacy>
</template>
<script setup lang="ts">
import PdPrivacy from './pd-privacy.vue';
import Store from '@/store';
import { useBottomSafeArea, useCustomHeader } from '@/pd-libs/hooks/useDisplay';
import PD from '@/pd-libs/libs';
import ApiHelp from '@/api/api-help';
import { APP_ROUTES } from '@/pd-libs/enum/app-routes';
const userStore = Store.user();
const { customHeaderHeightPx } = useCustomHeader(); //自定义头部高度
const { tabBarHeightPx } = useBottomSafeArea(); //tabbar高度

withDefaults(defineProps<{
	widthCustomHeaderPadding : boolean
}>(), {
	widthCustomHeaderPadding : false, //是否需要撑起自定义导航栏高度
});


interface EmitType {
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	(e: 'page-load', p?: any): void;//页面onload事件 
}
const emits = defineEmits<EmitType>();

//缓存页面入参，由于所有页面都会包含在该组件内，所以这里只处理可能存在的公共的页面参数
type baseLoadOps = {
	url ?: string,//重定向url
	inviteMark ?: string,//车队邀请码
	account_id ?: string,//分享发起者的钱包id
	driver_group_id ?: string,//合作车队邀请司机加入时携带的司机分组的id
	scene ?: string,//扫码进入小程序
}

type SceneParams = {
	gid ?: string,//合作车队分组二维码邀请司机携带的分组id
	m ?: string,//合作车队分组二维码邀请司机携带的邀请码
}

let cacheLoadParams: baseLoadOps | undefined = {};

onLoad(async (p?: baseLoadOps) => {
	p?.inviteMark && (Store.inviter().mark = p.inviteMark);//存储车队邀请码
	p?.account_id && (Store.inviter().account_id = Number(p.account_id));//存储分销发起者accoundid
	p?.driver_group_id && (Store.inviter().driver_group_id = Number(p.driver_group_id));

	if (p?.scene) {
		const sceneParams = PD.Tool.paramString2Obj<SceneParams>(decodeURIComponent(p.scene));
		sceneParams.gid && (Store.inviter().driver_group_id = Number(sceneParams.gid));
		sceneParams.m && (Store.inviter().mark = sceneParams.m);
	};

	cacheLoadParams = p;
});

//隐私协议同意，同意后再emit:onLoad 确保在用户同意协议后再继续后续逻辑
const privacyRead = async () => {
	if (userStore.loginFinished) {
		emits('page-load', cacheLoadParams);
		return;
	};
	//没登录过，进入登录逻辑，登录后触发page-load
	PD.UniHelp.loading();
	ApiHelp.User.userLogin().then(() => {
		//用户未注册
		if (!userStore.isReg) {
			emits('page-load', cacheLoadParams);
			return;
		};
		//账户异常
		if (userStore.isAccountError) {
			userStore.accountErrorData = {
				...userStore.driverLoginResponse.errdata,
				status : userStore.driverLoginResponse.status,
			};
			PD.UniHelp.redirectTo(APP_ROUTES.accountError);
			return;
		};
		//如果有重定向url，则重定向到目标页面
		if (cacheLoadParams?.url) {
			PD.UniHelp.reLaunch(decodeURIComponent(cacheLoadParams.url));
			return;
		};
		//如果在注销中，重定向到注销承接页
		if (userStore.driverLoginResponse.logoff_time) {
			PD.UniHelp.redirectTo(APP_ROUTES.accountCancellation);
			return;
		};
		emits('page-load', cacheLoadParams);
	}).finally(() => {
		try {
			PD.UniHelp.loading(false);
		} catch (e) { };
	});
};

defineExpose({
	test : () => {console.log(1);},
});

</script>

<style scoped lang="less">
.__padding_custom_header {
	padding-top: v-bind(customHeaderHeightPx);
}

.__padding_custom_tab_bar {
	padding-bottom: v-bind(tabBarHeightPx);
}
</style>